﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Prism.Games.GalEngine.Scripts.Parser
{
    public class GraphicBase
    {
        public GraphicBase()
        {
            this.GalUIFrame = new GalUIFrame();
        }

        /// <summary>
        /// 设置或者获取游戏窗口。
        /// </summary>
        public IGameWindow GameWindow { get; set; }

        public GalUIFrame GalUIFrame { get; set; }

        public void CloseGalWindow()
        {
            this.GameWindow.Close();
        }

        public void SetUIFrame(GalUIFrame frame)
        {
            this.GalUIFrame = frame;
        }

        public void SwicthFrame(IScene scene)
        {
            var currentFrame = this.GameWindow.GetContent();
            if (currentFrame == null)
                currentFrame = this.GalUIFrame.Animations;

            if (scene is TrademarkScene)
            {
                // Set content from galUIFrame
                if (currentFrame != this.GalUIFrame.TrademarkFrame)
                {
                    this.GameWindow.SetContent(this.GalUIFrame.TrademarkFrame);
                }
            }

            if (scene is MenuScene)
            {
                // set ...
                if (currentFrame != this.GalUIFrame.MenuFrame)
                {
                    this.GameWindow.SetContent(this.GalUIFrame.MenuFrame);
                }
            }

            if (scene is VideoScene)
            {
                if (currentFrame != this.GalUIFrame.VideoFrame)
                {
                    this.GameWindow.SetContent(this.GalUIFrame.VideoFrame);
                }
            }

            if (scene is GamingScene)
            {
                if (currentFrame != this.GalUIFrame.GamingFrame)
                {
                    this.GameWindow.SetContent(this.GalUIFrame.GamingFrame);
                }
            }
        }

        public object FindName(object element, string name)
        {
            if (element is System.Windows.FrameworkElement)
            {
                var target = element as System.Windows.FrameworkElement;
                return target.FindName(name);
            }
            else return null;
        }

        private System.Windows.Media.Animation.Storyboard storyboard;

        public void PlayAnimation(string key, Action playEndCallback)
        {
            if (this.storyboard != null)
            {
                this.storyboard.Stop();
            }

            try
            {
                this.storyboard = ((this.GalUIFrame.Animations as System.Windows.Controls.UserControl).FindResource(key) as System.Windows.Media.Animation.Storyboard).Clone();
                this.storyboard.Completed += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (playEndCallback != null)
                        playEndCallback();
                });


                this.storyboard.Begin(this.GameWindow.GetContent() as System.Windows.FrameworkElement);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void PlaySecondAnimation(string key, Action playEndCallback)
        {
            if (this.storyboard != null)
            {
                this.storyboard.Stop();
            }

            try
            {
                this.storyboard = ((this.GalUIFrame.Animations as System.Windows.Controls.UserControl).FindResource(key) as System.Windows.Media.Animation.Storyboard).Clone();
                this.storyboard.Completed += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (playEndCallback != null)
                        playEndCallback();
                });

                this.storyboard.Begin(this.GameWindow.GetSecondContent() as System.Windows.FrameworkElement);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void PlayMainAnimation(string key, Action playEndCallback)
        {
            if (this.storyboard != null)
            {
                this.storyboard.Stop();
            }

            this.storyboard = ((this.GalUIFrame.Animations as System.Windows.Controls.UserControl).FindResource(key) as System.Windows.Media.Animation.Storyboard).Clone();
            this.storyboard.Completed += new EventHandler(delegate(object sender, EventArgs e)
            {
                if (playEndCallback != null)
                    playEndCallback();
            });
            this.storyboard.Begin(this.GameWindow.GetMainContent() as System.Windows.FrameworkElement);
        }

        public void PlayThirdAnimation(string key, Action playEndCallback)
        {
            if (this.storyboard != null)
            {
                this.storyboard.Stop();
            }

            try
            {
                this.storyboard = ((this.GalUIFrame.Animations as System.Windows.Controls.UserControl).FindResource(key) as System.Windows.Media.Animation.Storyboard).Clone();
                this.storyboard.Completed += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (playEndCallback != null)
                        playEndCallback();
                });
                this.storyboard.Begin(this.GameWindow.GetThirdContent() as System.Windows.FrameworkElement);
            }
            catch (Exception)
            {
                return;
            }
        }

        public void PlayFourthAnimation(string key, Action playEndCallback)
        {
            if (this.storyboard != null)
            {
                this.storyboard.Stop();
            }

            try
            {
                this.storyboard = ((this.GalUIFrame.Animations as System.Windows.Controls.UserControl).FindResource(key) as System.Windows.Media.Animation.Storyboard).Clone();
                this.storyboard.Completed += new EventHandler(delegate(object sender, EventArgs e)
                {
                    if (playEndCallback != null)
                        playEndCallback();
                });
                this.storyboard.Begin(this.GameWindow.GetFourthContent() as System.Windows.FrameworkElement);
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                return;
            }
        }

        public void ShowLoadingFrame(Action loadingAction, Action loadingEndCallback)
        {
            var content = this.GameWindow.GetContent();

            this.GameWindow.SetContent(null);

            this.GameWindow.SetFourthContent(this.GalUIFrame.LoadingFrame);

            this.PlayFourthAnimation(GameFrameLibrary.Strings.LoadingFadeIn, null);

            if (loadingAction != null)
            {
                loadingAction.BeginInvoke(new AsyncCallback(delegate(IAsyncResult ar)
                {
                    if (ar.IsCompleted)
                    {
                        loadingAction.EndInvoke(ar);

                        var dis = this.GameWindow.GetDispatcher();

                        dis.Invoke(new Action(delegate()
                        {
                            this.PlayFourthAnimation(GameFrameLibrary.Strings.LoadingFadeOut, null);

                            this.GameWindow.SetFourthContent(null);

                            this.GameWindow.SetContent(content);

                            if (loadingEndCallback != null)
                                loadingEndCallback();

                        }), null);
                    }
                }), null);
            }
        }

        public void SetWindowConfig(WindowConfig config)
        {
            var target = new GameConfig(config);
            Helper.Parser.GameConfig = target;
            Helper.GameConfig = target;
        }
    }
}
